#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->ui->lcdNumber->display("00:00:00:000");
    this->ptimer = new QTimer(this);
    this->ui->btn_suspend->setEnabled(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_btn_start_clicked()
{
    if(this->ui->btn_stop->text()=="清零")
    {
        this->ui->btn_stop->setText("停止");
    }
    this->ui->btn_start->setEnabled(false);
    this->ui->btn_stop->setEnabled(true);
    this->ui->btn_suspend->setEnabled(true);
    this->baseTime = QTime::currentTime();
    connect(ptimer,&QTimer::timeout,this,[=](){
        //每隔1毫秒就显示一次
        QTime curTime = QTime::currentTime();
        int msec = baseTime.msecsTo(curTime);
        QTime showTime(0,0,0,0);
        showTime = showTime.addMSecs(msec);
        show_time_str = showTime.toString("hh:mm:ss:zzz");

        this->ui->lcdNumber->display(show_time_str);
    });

    ptimer->start(1);
}

void MainWindow::on_btn_stop_clicked()
{
    if(this->ui->btn_stop->text() == "停止")
    {
        this->ui->btn_suspend->setEnabled(false);
        this->ui->btn_start->setEnabled(true);
        this->ptimer->stop();
        this->ui->btn_stop->setText("清零");
        if(this->ui->btn_suspend->text()=="继续")
        {
            this->ui->btn_suspend->setText("暂停");
        }
    }else
    {
        this->ui->lcdNumber->display("00:00:00:000");
        this->ui->textBrowser->clear();
        this->ui->btn_stop->setText("停止");
    }
}

void MainWindow::on_btn_suspend_clicked()
{
    static QTime pauseTime;
    if(this->ui->btn_suspend->text() == "暂停")
    {
        pauseTime =  QTime::currentTime();
        this->ptimer->stop();
        this->ui->btn_suspend->setText("继续");
    }else
    {
        QTime curTime = QTime::currentTime();
        int msec = pauseTime.msecsTo(curTime); //时间差
        this->baseTime = this->baseTime.addMSecs(msec);
        this->ptimer->start(1);
        this->ui->btn_suspend->setText("暂停");
    }

}

void MainWindow::on_btn_dot_clicked()
{
    this->ui->textBrowser->append(show_time_str);
}
